package cn.cqautotest.cqviehelper.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;

import java.io.File;
import java.net.FileNameMap;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.base.BaseViewHolder;
import cn.cqautotest.cqviehelper.beans.FileInfo;
import cn.cqautotest.cqviehelper.utils.VerifyFileTypeUtils;

@SuppressLint({"NonConstantResourceId", "SimpleDateFormat"})
public class ItemRvFileListAdapter extends RecyclerView.Adapter<ItemRvFileListAdapter.InnerHolder> {

    private final List<FileInfo> mData = new ArrayList<>();
    private Context mContext;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");

    public void setData(List<FileInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_file_info, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        FileInfo fileInfo = mData.get(position);
        File file = fileInfo.getFile();
        ImageView ivFileFolderIcon = holder.ivFileFolderIcon;
        TextView tvFileFolderName = holder.tvFileFolderName;
        TextView tvFileFolderDetail = holder.tvFileFolderDetail;
        tvFileFolderName.setText(file.getName());
        if (file != null && file.exists()) {
            // TODO: 2020/11/16 是否为文件夹，文件夹：列出文件夹下文件的项数目及最后修改时间、文件：列出文件的大小及最后修改时间
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null) {
                    tvFileFolderDetail.setText(String.format("%s项 | %1s", files.length, sdf.format(file.lastModified())));
                }
            } else {
                // TODO: 2020/11/16 文件
                tvFileFolderDetail.setText(String.format("%s | %1s", FileUtils.getSize(file), sdf.format(file.lastModified())));
            }
        }
        String fileName = file.getName();
        if (!fileName.endsWith(".apk")) {
            Glide.with(mContext).load(VerifyFileTypeUtils.getIconByFile(file)).into(ivFileFolderIcon);
        }else {
            Drawable apkIcon = VerifyFileTypeUtils.getApkIcon(file.getPath());
            Glide.with(mContext).load(apkIcon).into(ivFileFolderIcon);
        }

        View itemView = holder.itemView;
        itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(fileInfo);
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(FileInfo fileInfo);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class InnerHolder extends BaseViewHolder {

        @BindView(R.id.ivFileFolderIcon)
        ImageView ivFileFolderIcon;
        @BindView(R.id.tvFileFolderName)
        TextView tvFileFolderName;
        @BindView(R.id.tvFileFolderDetail)
        TextView tvFileFolderDetail;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
